function listGoods() {
    $.ajax({
        url:"../goods.do",
        data:{"method":"listGoods"},
        dataType: "json",
        success:function (result) {
            //清空表数据
            $("tr").not(".tab_head").remove();
            $.each(result,function (index,data) {
                $("#table").append(initRow(data));
            });
        }
    });
}

function searchGoods(keyword,category,state) {
    $.ajax({
        url: "../goods.do",
        data: {"method": "searchGoods", "keyword": keyword, "category": category, "state": state},
        dataType: "json",
        success:function (result) {
            //清空表数据
            $("tr").not(".tab_head").remove();
            $.each(result,function (index,data) {
                $("#table").append(initRow(data));
            });
        }
    });
}

function initRow(good){
    return $("<tr><td>"
        +"<input type='checkbox' name='chs' value='0'>"
        +""+"</td><td>"
        +good.id+"</td><td>"
        +good.picture+"</td><td>"
        +good.name+"</td><td>"
        +good.color+"</td><td>"
        +good.price+"</td><td>"
        +good.inventory+"</td><td>"
        +good.sales+"</td><td>"
        +(good.state==1?'上架':'下架')+"</td><td> <a href='#' style='color: blue'>编辑</a><a href='javascript:del(\""+good.id+"\");' style='color: blue'>删除</a></td></tr>");
}

function del(goodsId) {
    $.ajax({
        url:"../goods.do",
        data:{"method":"deleteGoods","goodsId":goodsId},
        dataType:"text",
        type:"post",
        success:function (result) {
            if(result=='true'){
                listGoods();
            }else{
                alert("删除失败！")
            }
        }
    })
}

function  listCategory() {
    $.ajax({
        url:"../goods.do",
        data:{"method":"listCategory"},
        dataType: "json",
        success:function (result) {
            $.each(result,function (index,data) {
                $("#category").append(initCategory(data));
            });
        }
    });

    function initCategory(category){
        return $("<option value=\""+category.id+"\">"+category.name+"</option>");
    }
}


